﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Cryptography_0912089_0912100
{
    public partial class RSACryptoForm : Form
    {
        public RSACryptoForm()
        {
            InitializeComponent();
        }

        private void buttonSavePrivateKey_Click(object sender, EventArgs e)
        {
            if (this.saveFileDialogPrivateKey.ShowDialog() == DialogResult.OK)
            {
                this.textBoxPrivateKeySave.Text = this.saveFileDialogPrivateKey.FileName;
            }
        }

        private void buttonSavePublicKey_Click(object sender, EventArgs e)
        {
            if (this.saveFileDialogPublicKey.ShowDialog() == DialogResult.OK)
            {
                this.textBoxPublicKeySave.Text = this.saveFileDialogPublicKey.FileName;
            }
        }

        private void buttonGenKeyPair_Click(object sender, EventArgs e)
        {
            if(this.textBoxPassword.Text == "")
            {
                MessageBox.Show("You must enter password !!!",
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }
            if (this.textBoxPrivateKeySave.Text == "")
            {
                MessageBox.Show("You must save your private key !!!",
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }
            if (this.textBoxPublicKeySave.Text == "")
            {
                MessageBox.Show("You must save your public key !!!",
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }

            try
            {
                RSACryptopher rsaHelper = new RSACryptopher();

                File.WriteAllText(this.textBoxPrivateKeySave.Text, 
                                  rsaHelper.GeneratePrivateKey(this.textBoxPassword.Text),
                                  Encoding.UTF8);

                File.WriteAllText(this.textBoxPublicKeySave.Text,
                                  rsaHelper.GeneratePublicKey(this.textBoxPassword.Text),
                                  Encoding.UTF8);

                MessageBox.Show("Success !!!",
                                "Infomation",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message,
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }

        private void buttonBrowseKeyFile_Click(object sender, EventArgs e)
        {
            if (this.openFileDialogKey.ShowDialog() == DialogResult.OK)
            {
                this.textBoxOpenKeyFile.Text = this.openFileDialogKey.FileName;
            }
        }

        private void buttonBrowseFileToEncrypt_Click(object sender, EventArgs e)
        {
            if (this.openFileDialogAll.ShowDialog() == DialogResult.OK)
            {
                this.textBoxFileToEncrypt.Text = this.openFileDialogAll.FileName;
            }
        }

        private void buttonSaveEncryptedFile_Click(object sender, EventArgs e)
        {
            if (this.saveFileDialogEncrypted.ShowDialog() == DialogResult.OK)
            {
                this.textBoxEncryptedFIle.Text = this.saveFileDialogEncrypted.SelectedPath;
            }
        }

        private void buttonEncrypt_Click(object sender, EventArgs e)
        {
            if (textBoxOpenKeyFile.Text == "")
            {
                MessageBox.Show("No key to encrypt.",
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }
            if (textBoxFileToEncrypt.Text == "")
            {
                MessageBox.Show("No file to encrypt.",
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }
            if (textBoxEncryptedFIle.Text == "")
            {
                MessageBox.Show("Please choose where to save encrypted file.",
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }


            RSACryptopher rsaHelper = new RSACryptopher();

            rsaHelper.LoadFromXML(this.textBoxOpenKeyFile.Text);

            String[] input = new String[2];
            input[0] = this.textBoxFileToEncrypt.Text;
            input[1] = this.textBoxEncryptedFIle.Text;

            rsaHelper.Encrypt(input);

        }

        private void buttonKeyPairOpen_Click(object sender, EventArgs e)
        {
            if (this.openFileDialogKey.ShowDialog() == DialogResult.OK)
            {
                this.textBoxKeyPair.Text = this.openFileDialogKey.FileName;
            }
        }

        private void buttonFileToDecrypt_Click(object sender, EventArgs e)
        {
            if (this.openEncryptedFile.ShowDialog() == DialogResult.OK)
            {
                this.textBoxFileToDecrypt.Text = this.openEncryptedFile.FileName;
            }
        }

        private void buttonSaveDecryptedFile_Click(object sender, EventArgs e)
        {
            if (this.saveFileDialogEncrypted.ShowDialog() == DialogResult.OK)
            {
                this.textBoxSaveDecrypted.Text = this.saveFileDialogEncrypted.SelectedPath;
            }
        }

        private void buttonDecrypt_Click(object sender, EventArgs e)
        {
            if (textBoxKeyPair.Text == "")
            {
                MessageBox.Show("No key to decrypt.",
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }
            if (textBoxFileToDecrypt.Text == "")
            {
                MessageBox.Show("No file to encrypt.",
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }
            if (textBoxSaveDecrypted.Text == "")
            {
                MessageBox.Show("Please choose where to save decrypted file.",
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }


            RSACryptopher rsaHelper = new RSACryptopher();

            rsaHelper.LoadFromXML(this.textBoxKeyPair.Text);

            String[] input = new String[2];
            input[0] = this.textBoxFileToDecrypt.Text;
            input[1] = this.textBoxSaveDecrypted.Text;

            rsaHelper.Decrypt(input);
        }

    }
}
